package com.dzt.cloudreader.base;

import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.LayoutRes;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.interfaces.PerfectClickListener;

/**
 * Created by M02323 on 2017/12/15.
 * 本类添加标题和加载等待界面
 */

public abstract class TitleActivity extends FrameActivity{

	private LayoutInflater layoutInflater;
	private AnimationDrawable animationDrawable;
	protected View rootView;
	protected View subView;
	protected Toolbar toolbar;
	private LinearLayout llProgressBar;
	private View refresh;

	protected <T extends View> T getView(int id) {
		return (T) findViewById(id);
	}

	@Override
	public void setContentView(@LayoutRes int layoutResID) {
		super.setContentView(layoutResID);
		layoutInflater = LayoutInflater.from(this);
		rootView = layoutInflater.inflate(R.layout.activity_title, null, false);
		subView = layoutInflater.inflate(layoutResID, null, false);
		toolbar = rootView.findViewById(R.id.tool_bar);
		// content
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		subView.setLayoutParams(params);
		RelativeLayout mContainer = rootView.findViewById(R.id.container);
		mContainer.addView(subView);
		getWindow().setContentView(rootView);

		// 设置透明状态栏
		//StatusBarUtil.setColor(this, CommonUtils.getColor(R.color.colorTheme), 0);
		llProgressBar = getView(R.id.ll_progress_bar);
		refresh = getView(R.id.ll_error_refresh);
		ImageView img = getView(R.id.img_progress);

		// 加载动画
		animationDrawable = (AnimationDrawable) img.getDrawable();
		// 默认进入页面就开启动画
		if (!animationDrawable.isRunning()) {
			animationDrawable.start();
		}

		setToolBar();
		// 点击加载失败布局
		refresh.setOnClickListener(new PerfectClickListener() {
			@Override
			protected void onNoDoubleClick(View v) {
				showLoading();
				onRefresh();
			}
		});
		subView.setVisibility(View.GONE);
	}

	/**
	 * 设置titlebar
	 */
	protected void setToolBar() {
		toolbar.setTitle("");
		setSupportActionBar(toolbar);
		ActionBar actionBar = getSupportActionBar();
		if (actionBar != null) {
			//去除默认Title显示
			actionBar.setDisplayShowTitleEnabled(false);
			//true显示返回按钮,false隐藏返回按钮
			actionBar.setDisplayHomeAsUpEnabled(true);
			//actionBar.setHomeAsUpIndicator(R.mipmap.icon_back);
		}
		//设置返回按钮的icon
		toolbar.setNavigationIcon(R.drawable.selector_return_btn);
		//返回按钮的点击事件
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	public void setToolBarTitle(String text) {
		toolbar.setTitle(text);
		//baseBinding.tvTitle.setText(text);
	}

	public void setToolBarTitle(int resId) {
		setToolBarTitle(getString(resId));
		//baseBinding.tvTitle.setText(text);
	}

	protected void showLoading() {
		if (llProgressBar.getVisibility() != View.VISIBLE) {
			llProgressBar.setVisibility(View.VISIBLE);
		}
		// 开始动画
		if (!animationDrawable.isRunning()) {
			animationDrawable.start();
		}
		if (subView.getVisibility() != View.GONE) {
			subView.setVisibility(View.GONE);
		}
		if (refresh.getVisibility() != View.GONE) {
			refresh.setVisibility(View.GONE);
		}
	}

	protected void showContentView() {
		if (llProgressBar.getVisibility() != View.GONE) {
			llProgressBar.setVisibility(View.GONE);
		}
		// 停止动画
		if (animationDrawable.isRunning()) {
			animationDrawable.stop();
		}
		if (refresh.getVisibility() != View.GONE) {
			refresh.setVisibility(View.GONE);
		}
		if (subView.getVisibility() != View.VISIBLE) {
			subView.setVisibility(View.VISIBLE);
		}
	}

	protected void showError() {
		if (llProgressBar.getVisibility() != View.GONE) {
			llProgressBar.setVisibility(View.GONE);
		}
		// 停止动画
		if (animationDrawable.isRunning()) {
			animationDrawable.stop();
		}
		if (refresh.getVisibility() != View.VISIBLE) {
			refresh.setVisibility(View.VISIBLE);
		}
		if (subView.getVisibility() != View.GONE) {
			subView.setVisibility(View.GONE);
		}
	}

	/**
	 * 失败后点击刷新
	 */
	protected void onRefresh() {

	}
}
